package org.jeecg.modules.weixin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.reserve.entity.SdxcReserveUser;
import org.jeecg.modules.reserve.service.ISdxcReserveUserService;
import org.jeecg.modules.reserve.service.impl.SdxcReserveUserServiceImpl;
import org.jeecg.modules.weixin.constant.WxConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@Slf4j
@RestController
@Api(tags = "预约管理微信端")
@RequestMapping("/weixin")
class WxReserveController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    private ISdxcReserveUserService sdxcReserveUserService;


    /**
     * 微信小程序用户登录获取用户openid
     *
     * @param
     * @return
     */
    @GetMapping("/login")
    @ApiOperation(value = "用户授权登录", notes = "用户授权登录")
    public Result<?> login(@ApiParam(name = "code", value = "授权code", required = true) @RequestParam("code") String code) {
        String url = WxConstant.LOGIN_URL.replace("APPID", WxConstant.APP_ID).replace("SECRET", WxConstant.APP_SECRET).replace("JSCODE",code);

        log.info("授权登录请求url====={}" , url);
        String resStr = restTemplate.getForObject(url, String.class);
        JSONObject resJson = JSON.parseObject(resStr);
        log.info("授权登录响应========={}", resJson);

        if(null == resJson || resJson.containsKey("errcode")) {
            return Result.error("登录失败");
        }
        //  获取用户openid
        String openid = resJson.getString("openid");
        if(StringUtils.isBlank(openid)) {
            return Result.error("openid获取失败，请重试");
        }
        // 保存用户信息
//        String userId = userService.saveByOpenId(openid);
        return Result.ok(openid);

    }

    /**
     * 判断用户信息是否已完善
     *
     * @param
     * @return
     */
    @GetMapping("/queryUserStatus")
    public Result<?> queryUserStatus(@RequestParam("userId") String userId) {
        SdxcReserveUser sdxcReserveUser = sdxcReserveUserService.getById(userId);
        if (sdxcReserveUser != null && SdxcReserveUser.USER_INFO_FINISH_YES.equals(sdxcReserveUser.getIsFinish())) {
            // 用户信息已完善
            return Result.ok(SdxcReserveUser.USER_INFO_FINISH_YES);
        } else {
            // 用户不存在或信息未完善
            return Result.ok(SdxcReserveUser.USER_INFO_FINISH_NO);
        }
    }

    /**
     * 保存用户信息
     *
     * @param sdxcReserveUser
     * @return
     */
    @PutMapping("/saveUserInfo")
    public Result<?> saveUserInfo(@RequestBody SdxcReserveUser sdxcReserveUser) {
        if(StringUtils.isBlank(sdxcReserveUser.getId())) {
            return Result.error("用户ID为空");
        }
        SdxcReserveUser save = sdxcReserveUserService.getById(sdxcReserveUser.getId());
        if(null == save) {
            return Result.error("用户信息不存在");
        }
        // 状态为信息已完善
        sdxcReserveUser.setIsFinish(SdxcReserveUser.USER_INFO_FINISH_YES);
        sdxcReserveUserService.updateById(sdxcReserveUser);
        return Result.ok("保存成功!");
    }

}
